// 主函数，用于按顺序学习所有课程
async function learnCourses() {
    try {
        // 获取未完成的课程信息
        let courses = [];
        let payloadArray = [];
        courses = await getCourseDataXuan();
        let total = courses.length;
        for(var i=0;i<courses.length;i++){
	payloadArray[i] = await getPayload(courses[i].id);
        }
        console.log(payloadArray)
        let timer = setInterval(async ()=>{
        // 遍历课程列表并学习每个课程
        for (const payload of payloadArray) {
            await study(payloadArray);
        }
        
        if(payloadArray.length==0){
           clearInterval(timer);
           console.log("所有课程模拟学习完毕！");
        }else{
                console.log(`共${total}节课/目前学习完${payloadArray.length}节课`);
        }
        },3000);
    } catch (error) {
        console.error("学习课程时出错：", error);
    }
}

//获取选学课程信息
async function getCourseDataXuan() {
    return await fetch("https://www.xjgbzx.cn/trainee/api/course/uncompleted?currentPage=1&pageSize=100&year=2024", {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "no-cache",
            "content-type": "application/json",
            "sec-ch-ua": "\"Not:A-Brand\";v=\"99\", \"Chromium\";v=\"112\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "signature": "adfasfsdaffsdafsdafaj"
        },
        "referrer": "https://www.xjgbzx.cn/pc/index.html",
        "referrerPolicy": "origin-when-cross-origin",
        "body": "{}",
        "method": "POST",
        "mode": "cors",
        "credentials": "include"
    }).then(res => {
        let data = res.json();
        return data;
    }).then(data => {
        return data.data.courses;
    });
}

async function getPayload(courseID) {
    let payload = {};
    let playUrl = `https://www.xjgbzx.cn/trainee/api/course/play/${courseID}`;
    try {
        let playData = await fetch(playUrl).then(res => {
            return res.json();
        });
        payload.playCourse = playData.data.playCourse;
        let url = `https://www.xjgbzx.cn/trainee/api/course/detail/${courseID}`;
        let courseData = await fetch(url).then(res => {
            return res.json();
        });
        let info = courseData.data.course;
        payload.user_course_id = info.user_course_id;
        payload.scormData = [{
            "sco_id": "res01",
            "lesson_location": "1000",
            "session_time": 1000
        }];
 
        return payload;
    } catch (error) {
        console.error('Error fetching payload:', error);
        throw error; // 重新抛出错误，以便上层调用者可以处理
    }
}

async function study(payloadArray) {
            const res = await fetch("https://www.xjgbzx.cn/trainee/index/user_course", {
                headers: {
                    "accept": "application/json, text/plain, */*",
                    "accept-language": "zh-CN,zh;q=0.9",
                    "cache-control": "no-cache",
                    "content-type": "application/json",
                    "sec-ch-ua": "\"Not:A-Brand\";v=\"99\", \"Chromium\";v=\"112\"",
                    "sec-ch-ua-mobile": "?0",
                    "sec-ch-ua-platform": "\"macOS\"",
                    "sec-fetch-dest": "empty",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-site": "same-origin",
                    "signature": "adfasfsdaffsdafsdafaj" 
                },
                referrer: "https://www.xjgbzx.cn/pc/index.html",
                referrerPolicy: "origin-when-cross-origin",
                body: JSON.stringify(payloadArray[0]),
                method: "POST",
                mode: "cors",
                credentials: "include"
            });
            const data = await res.json();
 
            if (data.data.is_completed === 1) {
                console.log("学习完毕：", payloadArray[0].user_course_id);
                payloadArray.shift();
                return payloadArray;
            } else {
             console.log(`课程: ${payloadArray[0].user_course_id}==进度：${data.data.learning_progress}`);
            }

            
}


learnCourses();